package com.qingzhu.controller.goodsserver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingzhu.pojo.Specification;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.service.GoodsService;
import com.qingzhu.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectspecification")
public class SpecificationSelectController extends HttpServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("规格查询服务..");
        String serverName = req.getParameter("server");

        if (serverName != null && serverName.equals("findspecificationall")) {
            findSpecificationAll(req,resp);
        }else {
            resp.getWriter().write(objectMapper.writeValueAsString(new ResultVo(1001,"服务器类型标记为空")));
        }
    }

    // 查询所有的规格及规格参数
    public void findSpecificationAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Specification> specifications = goodsService.findSpecificationAll();
        ResultVo resultVo = new ResultVo(200,specifications);
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
